package com.clkj.xiuzheng.service.shangcheng;

import com.clkj.xiuzheng.dao.shangcheng.SCMenuMapper;
import com.clkj.xiuzheng.dao.shangcheng.ScCompanyMapper;
import com.clkj.xiuzheng.dao.shangcheng.ScOperatorMapper;
import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.Menu;
import com.clkj.xiuzheng.pojo.shangcheng.Operator;
import com.clkj.xiuzheng.pojo.shangcheng.ProductCompany;
import com.clkj.xiuzheng.pojo.shangcheng.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScLoginServiceImpl implements ScLoginService{

    @Autowired
    private ScCompanyMapper companyMapper;
    @Autowired
    private ScOperatorMapper scOperatorMapper;
    @Autowired
    private SCMenuMapper scMenuMapper;


    @Override
    public Result getFactory(String userName, String password, Integer loginType) {
        //公司登录
        if (loginType == 1){
            Example example = new Example(ProductCompany.class);
            example.createCriteria().andEqualTo("companyCount",userName);
            ProductCompany productCompany = companyMapper.selectOneByExample(example);
            if (productCompany == null){
            return new Result(false, StatusCode.ERROR, "用户名不存在或密码错误");
            }
            if (productCompany.getCompanyPassword().equals(password)){
                if (productCompany.getCompanyStatus() == 0){
                    return new Result(false, StatusCode.ERROR, "公司已禁用");
                }//此时为主公司登录
                if (productCompany.getParentId()==0){
                   Example example1 = new Example(Menu.class);
                   example1.createCriteria().andEqualTo("parentId",0);
                    List<Menu> menus = scMenuMapper.selectByExample(example1);
                    menus.forEach(menu -> {
                        Example example2 = new Example(Menu.class);
                        example2.createCriteria().andEqualTo("parentId",menu.getMenuId());
                        List<Menu> child = scMenuMapper.selectByExample(example2);
                        menu.setChildren(child);
                    });
                    LoginVo  loginVo =  new LoginVo();
                    loginVo.setMenuList(menus);
                    loginVo.setProductCompany(productCompany);
                    return new Result(true, StatusCode.OK, "登录成功",loginVo);
                }else {
                    //此时为推广公司登录
                    Menu menu = new Menu();
                    menu.setMenuId(20);
                    Menu menu1 = scMenuMapper.selectByPrimaryKey(menu);
                    Example example1 = new Example(Menu.class);
                    example1.createCriteria().andEqualTo("parentId",menu1.getMenuId());
                    List<Menu> menus = scMenuMapper.selectByExample(example1);
                    menu1.setChildren(menus);
                    List<Menu> menus1 = Arrays.asList(menu1);
                    LoginVo  loginVo =  new LoginVo();
                    loginVo.setMenuList(menus1);
                    loginVo.setProductCompany(productCompany);
                    return new Result(true, StatusCode.OK, "登录成功",loginVo);
                }

            }else {
                return new Result(false, StatusCode.ERROR, "用户名不存在或密码错误");
            }
        }else {
        //操作员登录
            Example example = new Example(Operator.class);
            example.createCriteria().andEqualTo("operatorAccount",userName);
            Operator operator = scOperatorMapper.selectOneByExample(example);
            if (operator == null){
                return new Result(false, StatusCode.ERROR, "用户名不存在或密码错误");
            }
            if (operator.getOperatorPassword().equals(password)){
                if (operator.getOperatorStatus() == 0){
                    return new Result(false, StatusCode.ERROR, "操作员已禁用");
                }
                if (operator.getIdentityId() == null){
                return new Result(false, StatusCode.ERROR, "操作员未分配权限");
                }
                else {
                Integer[] menuIds =  scOperatorMapper.selectMenuById(operator.getOperatorId());
                Example example1 = new Example(Menu.class);
                Example.Criteria criteria = example1.createCriteria();
                criteria.andIn("menuId", Arrays.asList(menuIds));
                List<Menu> menus = scMenuMapper.selectByExample(example1);
                    List<Menu> menus1 = menus.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
                    menus1.forEach(menu -> {
                        List<Menu> child = menus.stream().filter(menu1 -> menu1.getParentId() == menu.getMenuId()).collect(Collectors.toList());
                        menu.setChildren(child);
                    });
                    LoginVo  loginVo =  new LoginVo();
                    loginVo.setMenuList(menus1);
                    loginVo.setOperator(operator);
                    return new Result(true, StatusCode.OK, "登录成功",loginVo);
                }
            }else {
                return new Result(false, StatusCode.ERROR, "用户名不存在或密码错误");
            }
        }
    }
}
